/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telsoft.managedbean.function;

import com.telsoft.business.AttributesBusiness;
import com.telsoft.business.ChildrenBusiness;
import com.telsoft.entity.AttributesEntity;
import com.telsoft.entity.ChildrenEntity;
import com.telsoft.entity.ComponentEntity;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author thieplv
 */
@ManagedBean(name = "rightCenterBean")
@ViewScoped
public class RightCenterBean implements Serializable{

    private List<ChildrenEntity> children;
    private List<AttributesEntity> attrs;
    private HashMap<ChildrenEntity, List<AttributesEntity>> hash;
    //getter

    public List<ChildrenEntity> getChildren() {
        return children;
    }

    public List<AttributesEntity> getAttrs() {
        return attrs;
    }
    //setter

    public void setChildren(List<ChildrenEntity> children) {
        this.children = children;
    }

    public void setAttrs(List<AttributesEntity> attrs) {
        this.attrs = attrs;
    }
    //contructor

    public RightCenterBean() throws SQLException {
        FacesContext context = FacesContext.getCurrentInstance();
        LeftProcessBean leftInstance = (LeftProcessBean) context.getApplication().evaluateExpressionGet(context, "#{leftInstance}", LeftProcessBean.class);
        hash = new HashMap<ChildrenEntity, List<AttributesEntity>>();
        children = new ArrayList<ChildrenEntity>();
        attrs = new ArrayList<AttributesEntity>();
        for (Iterator<ChildrenEntity> it = ChildrenBusiness.getChildren().iterator(); it.hasNext();) {
            ChildrenEntity itemChild = it.next();
            if (leftInstance.getComSelected().isLeaf()) {
                ComponentEntity comp = (ComponentEntity) leftInstance.getComSelected().getData();
                if (itemChild.getCompID() == comp.getComID()) {
                    children.add(itemChild);
                }
            }

        }
        for (ChildrenEntity item : children) {
            for (Integer itemInt : item.getAttID()) {
                for (AttributesEntity itemAtt : AttributesBusiness.getAttrs()) {
                    if (itemAtt.getAttID() == itemInt) {
                        attrs.add(itemAtt);
                    }
                }
            }
            hash.put(item, attrs);
        }
    }
    //
}
